package com.bestescortbedsystem.entity;

import com.bestescortbedsystem.dao.SesBedMessageDao;
import com.bestescortbedsystem.dao.SesHospitalinfoDao;
import com.bestescortbedsystem.dao.SesHospitalinfoDepartmentDao;
import com.bestescortbedsystem.dao.SesUserOrderDao;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @Author: LuXi
 * @Date: 2021/2/26 14:41
 * @Description: 获取用户详情工具类
 */
public class GetUserDetails {

    public static List<SesUser> getUserDetails(List<SesUser> list, SesUserOrderDao sesUserOrderDao, SesBedMessageDao sesBedMessageDao, SesHospitalinfoDao sesHospitalinfoDao, SesHospitalinfoDepartmentDao sesHospitalinfoDepartmentDao){
        for (SesUser s:list) {
            //获取用户Id
            Long userid = s.getUserid();
            //查询当前用户订单数量
            SesUserOrderExample exampleOrder = new SesUserOrderExample();
            SesUserOrderExample.Criteria criteriaOrder = exampleOrder.createCriteria();
            criteriaOrder.andUserUseridEqualTo(userid);
            //查询当前用户拥有的订单
            List<SesUserOrder> orderList = sesUserOrderDao.selectByExample(exampleOrder);
            Integer size = orderList.size();//每个用户的订单数量
            s.setCount(size);//当前用户订单数量

            //累加获取消费金额
            Float sum = 0F;
            for (SesUserOrder so:orderList) {
                sum += so.getUserorderrent();
            }
            String format = new DecimalFormat("##.##").format(sum);
            s.setMoney(Float.valueOf(format));

            //查询下单较多的医院
            //通过当前用户Id 查询用户租借陪护床的信息
            SesBedMessageExample bedExample = new SesBedMessageExample();
            SesBedMessageExample.Criteria bedCriteria = bedExample.createCriteria();
            bedCriteria.andUseridEqualTo(userid);

            //当前用户租借陪护床集合
            List<SesBedMessage> beds = sesBedMessageDao.selectByExample(bedExample);

            //通过循环查询出现最多的医院Id和科室Id
            Map<Long,Integer> hospitalMap = new HashMap<>();
            Map<Long,Integer> departmentMap = new HashMap<>();
            for (SesBedMessage sbm:beds) {
                //获得医院Id
                Long hid = sbm.getHospitalinfoid();
                if(!hospitalMap.containsKey(hid)){
                    hospitalMap.put(hid,1);
                }else {
                    hospitalMap.put(hid,hospitalMap.get(hid) + 1);
                }
                //获得科室Id
                Long did = sbm.getHospitalinfoDepartmentid();
                if(!departmentMap.containsKey(did)){
                    departmentMap.put(did,1);
                }else{
                    departmentMap.put(did,departmentMap.get(did) + 1);
                }
            }
            //获取当前用户下单最多的医院
            Set<Long> hids = hospitalMap.keySet();
            Iterator<Long> itHospital = hids.iterator();
            Long maxHid = 0L;
            int flag = 0;
            while (itHospital.hasNext()){
                Long key = itHospital.next();
                Integer val = hospitalMap.get(key);
                if(val > flag){
                    flag = val;
                    maxHid = key;
                }
            }
            //获取当前用户下单最多的科室
            Set<Long> dids = departmentMap.keySet();
            Iterator<Long> itDepartment = dids.iterator();
            Long maxDid = 0L;
            int temp = 0;
            while (itDepartment.hasNext()){
                Long key = itDepartment.next();
                Integer val = departmentMap.get(key);
                if(val > temp){
                    temp = val;
                    maxDid = key;
                }
            }
            if(maxDid!=0&&maxHid!=0){
                //查询医院和科室名称
                SesHospitalinfo hospitalinfo = sesHospitalinfoDao.selectByPrimaryKey(maxHid);
                SesHospitalinfoDepartment department = sesHospitalinfoDepartmentDao.selectByPrimaryKey(maxDid);
                //赋值
                s.setHospital(hospitalinfo.getHospitalinfoname()+"  "+department.getDepartmentname());
            }else{
                s.setHospital("无");
            }
        }
        return list;
    }

}
